package com.chenfan.mcn.utils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.chenfan.mcn.utils.handle.BtSheetWriteHandler;
import com.chenfan.mcn.utils.handle.CustomCellWriteHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.springframework.http.HttpHeaders;

import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.util.List;

/**
 * @ClassName ExcelUtilWithEasyExcel
 * @Description TODO
 * @Author Yutao.Lin
 * @Date 5/3/23 10:35 PM
 * @Version 1.0
 **/
@Slf4j
public class ExcelUtilWithEasyExcel {

    public static void exportExcelByEasyExcel(List<?> data, String title, String sheetName, Class<?> clazz, String fileName, HttpServletResponse response){
        try {
            int fieldNum = 0;
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields){
                boolean annotationPresent = field.isAnnotationPresent(ExcelProperty.class);
                if (annotationPresent){
                    fieldNum ++;
                }
            }
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setCharacterEncoding("utf-8");
            String encodedFileName = URLEncoder.encode(fileName, "UTF-8");
            response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + encodedFileName);
            ExcelWriterSheetBuilder excelWriterSheetBuilder = EasyExcel.write(response.getOutputStream(), clazz)
                    .sheet(sheetName)
                    .registerWriteHandler(new CustomCellWriteHandler());
            if (StringUtils.isNotBlank(title)){
                excelWriterSheetBuilder.registerWriteHandler(new BtSheetWriteHandler(title, fieldNum - 1, Boolean.FALSE)).useDefaultStyle(true).relativeHeadRowIndex(1);
            }else{
                excelWriterSheetBuilder.registerWriteHandler(buildHeightAndFont());
            }
            excelWriterSheetBuilder.doWrite(data);
        } catch (Exception e) {
            throw new RuntimeException(fileName + "下载文件失败", e);
        }
    }


    private static HorizontalCellStyleStrategy buildHeightAndFont() {
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short) 11);
        headWriteFont.setBold(true);
        headWriteCellStyle.setWriteFont(headWriteFont);
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        WriteFont contentWriteFont = new WriteFont();
        contentWriteFont.setFontHeightInPoints((short) 11);
        contentWriteCellStyle.setWriteFont(contentWriteFont);
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        contentWriteCellStyle.setWrapped(true);
        return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
    }
}